###########################################################################
# Lipps & Sczepanski & Malet
# Understanding Preferences over Borders
# International Studies Quaterly

# Main: Conjoint experiment -----------------------------------------------
###########################################################################

rm(list = ls())

packages <- c("tidyverse", "cregg", "ggplot2")

for (package in packages) {
  if (!requireNamespace(package, quietly = TRUE)) {
    install.packages(package, dependencies = TRUE)
  }
}

library(tidyverse)
library(ggplot2)
library(cregg)

#Load data
dat_long <- read.csv("survey_dat_clean_conjoint.csv", na.strings=c("","NA"))
dat <- read.csv("survey_dat_clean.csv", na.strings=c("","NA"))

#recode
dat_long$border <- as.factor(dat_long$border)
dat_long$country <- as.factor(dat_long$country)
dat_long$aim <- as.factor(dat_long$aim)
dat_long$open_cat <- ordered(dat_long$open_cat, levels=c("open", "restricted", "closed"))
dat_long$rati <- as.numeric(dat_long$rati)

#Figure 3: MAIN MODEL - Rating ---------------------------------------------
results_rati_main <- cregg::cj(rati~ border+aim+country, data=dat_long,
                                 id=~id, estimate="mm", h0=5)

#make labels for plots
labels<- as_labeller(c("border" = "Measure", "aim"="Aim", "country"="Country"))

pdf(file = "fig_3.pdf",width = 8,height = 5,onefile=F)

ggplot(results_rati_main)+
  geom_point(aes(x=estimate, y=level), size=2)+
  geom_errorbar(aes(xmin=lower, xmax=upper, y=level), width=0, linewidth=1)+
  facet_grid(feature~., scales = "free", labeller = labels)+
  geom_vline(xintercept = 5, linetype="dotted")+
  labs(x="Marginal Means = Rating", y="")+
  theme_minimal() +
  theme(
    axis.text.y.left = element_text(size = 16),
    axis.text.x = element_text(size=16),
    axis.title.y= element_text(size = 16),
    axis.title.x = element_text(size=16),
    strip.text.y.right = element_text(size=16))

dev.off()

#Figure 4: Subgroup analysis by border preferences
results_rat_open<- cregg::cj(rati ~ border+aim+country, data=dat_long,
                             id=~id, estimate="mm", h0=5, by=~open_cat)

pdf(file = "fig_4.pdf",width = 8,height = 5,onefile=F)

ggplot(results_rat_open)+
  geom_point(aes(x=estimate, y=level, color=open_cat, shape=open_cat), size=2)+
  geom_errorbar(aes(xmin=lower, xmax=upper, y=level, color=open_cat), width=0, size=1)+
  facet_grid(feature~., scales = "free", labeller = labels)+
  geom_vline(xintercept = 5, linetype="dotted")+
  labs(x="Marginal Mean = Rating", y="", color="Border closure")+
  scale_color_manual(values=c("deepskyblue3", "darkgoldenrod1", "red3"), name="")+
  scale_shape_manual(values=c(16,15,17), name="") +
  theme_minimal() +
  theme(
    axis.text.y.left = element_text(size = 16),
    axis.text.x = element_text(size=16),
    axis.title.y= element_text(size = 16),
    axis.title.x = element_text(size=16),
    strip.text.y.right = element_text(size=16),
    legend.text = element_text(size=16))

dev.off()

#Figure 5: Subgroup analysis by vote intention
dat_long$vote_intention <- ordered(dat_long$vote_intention, levels = c("The Left", "SPD", "Greens", "FDP", "CDU/CSU", "AfD", "Other", "No show", "Don't know"))
results_rat_vote <- cregg::cj(rati ~ border+aim+country, data=dat_long,
                              id=~id, estimate="mm", h0=5, by=~vote_intention)

pdf(file = "fig_5.pdf",width = 8,height = 5,onefile=F)

results_rat_vote %>% filter(vote_intention!="Don't know", vote_intention!="No show", vote_intention!="Other") %>%
  ggplot()+
  geom_point(aes(x=estimate, y=level, color=vote_intention), size=2, position = position_dodge(0.4))+
  geom_errorbar(aes(xmin=lower, xmax=upper, y=level, color=vote_intention), width=0, size=1, position = position_dodge(0.4))+
  facet_grid(feature~., scales = "free", labeller = labels)+
  geom_vline(xintercept = 5, linetype="dotted")+
  labs(x="Marginal Means = Rating", y="", color="Vote intention")+
  scale_color_manual(values=c("violetred1", "red3", "forestgreen", "darkgoldenrod1", "black", "deepskyblue3"))+
  theme_minimal() +
  theme(
    axis.text.y.left = element_text(size = 16),
    axis.text.x = element_text(size=16),
    axis.title.y= element_text(size = 16),
    axis.title.x = element_text(size=16),
    strip.text.y.right = element_text(size=16),
    legend.text = element_text(size=16),
    legend.title = element_text(size=16))

dev.off()
